package top.xiaoshuaila.moments.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.xiaoshuaila.moments.mapper.FocuMapper;
import top.xiaoshuaila.moments.mapper.MessageMapper;
import top.xiaoshuaila.moments.pojo.RespBean;
import top.xiaoshuaila.moments.utils.Dateutils;

import java.util.Objects;

@Service
public class FocuServiceImpl implements FocuService {
    @Autowired
    private FocuMapper focuMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public RespBean getfans(long id, long vid,int page) {
        return RespBean.success("success",focuMapper.getfans(id,vid,page*5,5));
    }

    @Override
    public boolean ispinbi(long id, long vid) {
        return focuMapper.ispinbi(id,vid);
    }

    @Override
    public void setblack(long id, long vid) {
        focuMapper.setblack(id,vid);
    }

    @Override
    public void removeblack(long id, long vid) {
        focuMapper.removeblack(id,vid);
    }

    @Override
    public int guanzhu(long id, long vid) {
        return focuMapper.guanzhu(id,vid);
    }

    @Override
    public void removefocuto(long id, long vid) {
        if(redisTemplate.opsForValue().get("id-" + vid)!=null)
        redisTemplate.delete(Objects.requireNonNull(redisTemplate.opsForValue().get("id-" + vid)));
        redisTemplate.delete("id-"+vid);
        focuMapper.removefocuto(id,vid);
        messageMapper.deleteMessagesfocu(id,vid);
    }

    @Override
    public void addfocuto(long id, long vid,String realname) {
        if(redisTemplate.opsForValue().get("id-" + vid)!=null)
        redisTemplate.delete(Objects.requireNonNull(redisTemplate.opsForValue().get("id-" + vid)));
        redisTemplate.delete("id-"+vid);
        focuMapper.addfocuto(id,vid,Dateutils.getDatetime());
        messageMapper.addMessagesfocu(1,vid,"用户“"+realname+"”关注了您！",
                Dateutils.getDatetime(),id);
    }

    @Override
    public RespBean getfocuto(long id, long vid,int page) {
        return RespBean.success("success",focuMapper.getfocuto(id,vid,page*5,5));
    }

    @Override
    public int gettotalfocuto(long id,long vid) {
        return focuMapper.gettotalfocuto(id,vid);
    }

    @Override
    public int gettotalfans(long id, long vid) {
        return focuMapper.gettotalfans(id,vid);
    }
}
